import type { ReactNode } from 'react'
import { getStorage } from '@/utils/storage'

interface AuthCodeNode {
    title: string;
    key: string;
    children?: AuthCodeNode[];
  }
export const authCode: AuthCodeNode[] = [{
  title: '订单管理',
  key: 'order',
  children: [{
    title: '订单列表',
    key: 'order-list',
    children: [{
      title: '添加',
      key: 'order-list-add'
    }, {
      title: '编辑',
      key: 'order-list-edit'
    }, {
      title: '删除',
      key: 'order-list-delete'
    }, {
      title: '指派',
      key: 'order-list-dispatch'
    }, {
      title: '批量指派',
      key: 'order-list-dispatch-batch-biz'
    }, {
    //   title: '批量邀约',
    //   key: 'order-list-dispatch-batch'
    // }, {
      title: '坐席转派',
      key: 'order-list-dispatch-set'
    }, {
      title: '导出',
      key: 'order-list-export'
    }]
  }, {
    title: '邀约订单',
    key: 'invite-list',
    children: [{
      title: '添加',
      key: 'invite-list-add'
    }, {
      title: '编辑',
      key: 'invite-list-edit'
    }, {
      title: '删除',
      key: 'invite-list-delete'
    }, {
      title: '指派',
      key: 'invite-list-dispatch'
    }, {
      title: '批量指派',
      key: 'invite-list-dispatch-batch-biz'
    }, {
      title: '坐席转派',
      key: 'invite-list-dispatch-set'
    }, {
      title: '邀约',
      key: 'invite-list-dispatch-invite'
    }, {
      title: '批量邀约',
      key: 'invite-list-dispatch-batch'
    }, {
      title: '邀约失败',
      key: 'invite-list-dispatch-invite-fail'
    }, {
      title: '导出',
      key: 'invite-list-export'
    }]
  }, {
    title: '坐席订单',
    key: 'seat-order',
    children: [{
      title: '添加',
      key: 'seat-order-add'
    }, {
      title: '编辑',
      key: 'seat-order-edit'
    }, {
      title: '删除',
      key: 'seat-order-delete'
    }, {
      title: '指派',
      key: 'seat-order-dispatch'
    }, {
      title: '坐席转派',
      key: 'seat-order-dispatch-set'
    }, {
      title: '邀约',
      key: 'seat-order-dispatch-invite'
    }, {
      title: '批量邀约',
      key: 'seat-order-dispatch-batch'
    }, {
      title: '邀约失败',
      key: 'seat-order-dispatch-invite-fail'
    }, {
      title: '导出',
      key: 'seat-order-export'
    }]
  }, {
    title: '意向订单',
    key: 'intentional-order',
    children: [{
      title: '添加',
      key: 'intentional-order-add'
    }, {
      title: '编辑',
      key: 'intentional-order-edit'
    }, {
      title: '删除',
      key: 'intentional-order-delete'
    }, {
      title: '指派',
      key: 'intentional-order-dispatch'
    }, {
    //   title: '批量邀约',
    //   key: 'intentional-order-dispatch-batch'
    // }, {
      title: '坐席转派',
      key: 'intentional-list-dispatch-set'
    }, {
      title: '导出',
      key: 'intentional-order-export'
    }]
  }, {
    title: '回收订单',
    key: 'recycling-order',
    children: [{
      title: '添加',
      key: 'recycling-order-add'
    }, {
      title: '编辑',
      key: 'recycling-order-edit'
    }, {
      title: '删除',
      key: 'recycling-order-delete'
    }, {
      title: '指派',
      key: 'recycling-order-dispatch'
    }, {
    //   title: '批量邀约',
    //   key: 'recycling-order-dispatch-batch'
    // }, {
      title: '坐席转派',
      key: 'recycling-list-dispatch-set'
    }, {
      title: '导出',
      key: 'recycling-order-export'
    }]
  }, {
    title: '待确认订单',
    key: 'confirmation-order',
    children: [{
      title: '添加',
      key: 'confirmation-order-add'
    }, {
      title: '编辑',
      key: 'confirmation-order-edit'
    }, {
      title: '删除',
      key: 'confirmation-order-delete'
    }, {
      title: '指派',
      key: 'confirmation-order-dispatch'
    }, {
    //   title: '批量邀约',
    //   key: 'confirmation-order-dispatch-batch'
    // }, {
      title: '坐席转派',
      key: 'confirmation-list-dispatch-set'
    }, {
      title: '导出',
      key: 'confirmation-order-export'
    }]
  }]
},{
  title: '运营管理',
  key: 'operation',
  children: [{
    title: '业务类型',
    key: 'business-list',
    children: [{
      title: '编辑',
      key: 'business-list-edit'
    }, {
      title: '启停',
      key: 'business-list-switch'
    }, {
      title: '添加',
      key: 'business-list-add'
    }]
  }, {
    title: '套餐管理',
    key: 'combo-list',
    children: [{
      title: '编辑',
      key: 'combo-list-edit'
    }, {
      title: '启停',
      key: 'combo-list-switch'
    }, {
      title: '添加',
      key: 'combo-list-add'
    }]
  }, {
    title: '礼品管理',
    key: 'gift-list',
    children: [{
      title: '编辑',
      key: 'gift-list-edit'
    }, {
      title: '启停',
      key: 'gift-list-switch'
    }, {
      title: '添加',
      key: 'gift-list-add'
    }]
  }]
},{
  title: '数据分析',
  key: 'analysis',
  children: [{
    title: '数据统计',
    key: 'analysis-count'
  }, {
    title: '数据分析',
    key: 'analysis-analysis'
  }]
},{
  title: '文件管理',
  key: 'ossfile',
  children: [{
    title: '删除',
    key: 'ossfile-delete'
  }, {
    title: '下载',
    key: 'ossfile-download'
  }, {
    title: '批量下载',
    key: 'ossfile-download-batch'
  }]
},{
  title: '系统设置',
  key: 'auth',
  children: [{
    title: '全局配置',
    key: 'global-config'
  }, {
    title: '角色管理',
    key: 'role-list',
    children: [{
      title: '添加',
      key: 'role-list-add'
    }, {
      title: '编辑',
      key: 'role-list-edit'
    }, {
      title: '启停',
      key: 'role-list-switch'
    }]
  }, {
    title: '团队管理',
    key: 'team-list',
    children: [{
      title: '编辑',
      key: 'team-list-edit'
    }, {
      title: '启停',
      key: 'team-list-switch'
    }, {
      title: '添加',
      key: 'team-list-add'
    }]
  }, {
    title: '用户管理',
    key: 'user-list',
    children: [{
      title: '编辑',
      key: 'user-list-edit'
    }, {
      title: '删除',
      key: 'user-list-delete'
    }, {
      title: '增加',
      key: 'user-list-add'
    }, {
      title: '启停',
      key: 'user-list-switch'
    }]
  }]
}]

export const authority = (key: string, node?: ReactNode, url?: string): ReactNode | boolean => {
  const userInfo = getStorage('user-info')
  const permissionCodes = (userInfo?.permissionCodes || []) as string[]
  const userCode = userInfo.userCode
  if (userCode === 'admin') {
    return node || true
  }
  let hasPermission = false
  const module = url?.split('/')[2]
  if (module) {
    hasPermission = permissionCodes.filter(code => {
      return code.indexOf(module) > -1
    }).includes(key)
  } else {
    hasPermission = permissionCodes.includes(key)
  }
  return hasPermission ? (node || true) : false
}